﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Main : MonoBehaviour
{
    public GameObject item;

    Transform[,] cells = new Transform[4,4];//16个格
    int[,] num = new int[4, 4];
    int count = 0,r1,r2,up,down,left,right;

    Vector3 startPosition;
    Vector3 endPosition;

    bool isup = true, isdown = true, isleft = true, isright = true;

    enum MouseDirection
    {
        Up,
        Down,
        Left,
        Right,
    }
    MouseDirection Dir;
    void Start()
    {
        InitCell();

        RandomItem();
        RandomItem();


    }
    private void Update()
    {
        GetInputDirection();

        MoveDirection();
    }
    /// <summary>
    /// 获取所有数格
    /// </summary>
    void InitCell()
    {
        Transform trans = transform.Find("bg");
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++, count++)
            {
                cells[i, j] = trans.GetChild(count).GetComponent<Transform>();
            }
        }
    }
    /// <summary>
    /// 随机2个数，随机分配2和4
    /// </summary>
    void RandomItem()
    {
        r1 = Random.Range(0, 4);
        r2 = Random.Range(0, 4);
        if (cells[r1,r2].childCount != 0)
        {
            RandomItem();
        }
        else
        {
            GameObject g1 = Instantiate(item, cells[r1, r2]);
            g1.name = item.name;
            float s1 = Mathf.Pow(2, Random.Range(1, 3));
            g1.transform.Find("Text").GetComponent<Text>().text = s1.ToString();
        }
    }
    /// <summary>
    /// 获取鼠标滑动方向
    /// </summary>
    void GetInputDirection()
    {
        if (Input.GetMouseButtonDown(0))
        {
            up = down = left = right = 0;
             startPosition = Input.mousePosition;
        }

        if (Input.GetMouseButton(0))
        {
            endPosition = Input.mousePosition - startPosition;
            if (endPosition.magnitude > 20)
            {
                //Vector3.Dot获得点积
                //点积：
                //点积的计算方式为: a·b =| a |·| b | cos < a,b > 其中 | a | 和 | b | 表示向量的模，
                //< a,b > 表示两个向量的夹角。另外在 点积 中，< a,b > 和 < b,a > 夹角是不分顺序的。 
                //所以通过点积，我们其实是可以计算两个向量的夹角的。 
                //另外通过点积的计算我们可以简单粗略的判断当前物体是否朝向另外一个物体:
                //只需要计算当前物体的transform.forward向量与(otherObj.transform.position 
                //– transform.position)的点积即可， 大于0则面对，否则则背对着。当然这个计算也会有一点误差，但大致够用。 

                //然后取它的反余弦，获取到角度
                //Mathf.Acos(f);
                // 以弧度为单位计算并返回参数 f 中指定的数字的反余弦值。

                //在然后Mathf.Rad2（弧度转为度数）
                var angleY = Mathf.Acos(Vector3.Dot(endPosition.normalized, Vector2.up)) * Mathf.Rad2Deg;
                var angleX = Mathf.Acos(Vector3.Dot(endPosition.normalized, Vector2.right)) * Mathf.Rad2Deg;
                //判断鼠标在对X和Y的正方向的夹角
                if (angleY <= 45)
                {
                    Dir = MouseDirection.Up;
                    up++;
                }
                else if (angleY >= 135)
                {
                    Dir = MouseDirection.Down;
                    down++;
                }
                else if (angleX <= 45)
                {
                    Dir = MouseDirection.Right;
                    right++;
                }
                else if (angleX >= 135)
                {
                    Dir = MouseDirection.Left;
                    left++;
                }
                print(Dir);
            }
        }
    }
    /// <summary>
    /// 移动滑动方向
    /// </summary>
    void MoveDirection()
    {
        if (up == 10&&isup)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int k = 0; k < 3; k++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (cells[j, i].childCount == 0 && cells[j + 1, i].childCount != 0)
                        {
                            Transform trans = cells[j + 1, i].GetChild(0);
                            trans.parent = cells[j, i];
                            trans.localPosition = Vector3.zero;
                        }
                        else if (cells[j, i].childCount != 0 && cells[j + 1, i].childCount != 0)
                        {
                            Text t1 = cells[j, i].GetChild(0).Find("Text").GetComponent<Text>();
                            Text t2 = cells[j + 1, i].GetChild(0).Find("Text").GetComponent<Text>();
                            int a = int.Parse(t1.text);
                            int b = int.Parse(t2.text);
                            if (a == b)
                            {
                                a += b;
                                t1.text = a.ToString();
                                Destroy(cells[j + 1, i].GetChild(0).gameObject);
                            }
                        }
                    }
                }
            }

            RandomItem();
            RandomItem();
            CheckDirection();
        }
        else if (down == 10&&isdown)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int k = 0; k < 3; k++)
                {
                    for (int j = 3; j >0; j--)
                    {
                        if (cells[j, i].childCount == 0 && cells[j - 1, i].childCount != 0)
                        {
                            Transform trans = cells[j - 1, i].GetChild(0);
                            trans.parent = cells[j, i];
                            trans.localPosition = Vector3.zero;
                        }
                        else if (cells[j, i].childCount != 0 && cells[j - 1, i].childCount != 0)
                        {
                            Text t1 = cells[j, i].GetChild(0).Find("Text").GetComponent<Text>();
                            Text t2 = cells[j - 1, i].GetChild(0).Find("Text").GetComponent<Text>();
                            int a = int.Parse(t1.text);
                            int b = int.Parse(t2.text);
                            if (a == b)
                            {
                                a += b;
                                t1.text = a.ToString();
                                Destroy(cells[j - 1, i].GetChild(0).gameObject);
                            }
                        }
                    }
                }
            }

            RandomItem();
            RandomItem();
            CheckDirection();
        }
        else if (left == 10&&isleft)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int k = 0; k < 3; k++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (cells[i, j].childCount == 0 && cells[i, j + 1].childCount != 0)
                        {
                            Transform trans = cells[i, j + 1].GetChild(0);
                            trans.parent = cells[i, j];
                            trans.localPosition = Vector3.zero;
                        }
                        else if (cells[i, j].childCount != 0 && cells[i, j + 1].childCount != 0)
                        {
                            Text t1 = cells[i, j].GetChild(0).Find("Text").GetComponent<Text>();
                            Text t2 = cells[i, j + 1].GetChild(0).Find("Text").GetComponent<Text>();
                            int a = int.Parse(t1.text);
                            int b = int.Parse(t2.text);
                            if (a == b)
                            {
                                a += b;
                                t1.text = a.ToString();
                                Destroy(cells[i, j + 1].GetChild(0).gameObject);
                            }
                        }
                    }
                }
            }

            RandomItem();
            RandomItem();
            CheckDirection();
        }
        else if (right == 10&&isright)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int k = 0; k < 3; k++)
                {
                    for (int j = 3; j > 0; j--)
                    {
                        if (cells[i, j].childCount == 0 && cells[i, j - 1].childCount != 0)
                        {
                            Transform trans = cells[i, j - 1].GetChild(0);
                            trans.parent = cells[i, j];
                            trans.localPosition = Vector3.zero;
                        }
                        else if (cells[i, j].childCount != 0 && cells[i, j - 1].childCount != 0)
                        {
                            Text t1 = cells[i, j].GetChild(0).Find("Text").GetComponent<Text>();
                            Text t2 = cells[i, j - 1].GetChild(0).Find("Text").GetComponent<Text>();
                            int a = int.Parse(t1.text);
                            int b = int.Parse(t2.text);
                            if (a == b)
                            {
                                a += b;
                                t1.text = a.ToString();
                                Destroy(cells[i, j - 1].GetChild(0).gameObject);
                            }
                        }
                    }
                }
            }

            RandomItem();
            RandomItem();
            CheckDirection();
        }
        else if (!isup && !isdown && !isleft && !isright)
        {
            print("游戏结束");
        }
    }
    /// <summary>
    /// 检测能否移动
    /// </summary>
    void CheckDirection()
    {
        if (up == 10)
        {
            
        }
        else if (down == 10)
        {
           
        }
        else if (left == 10)
        {
           
        }
        else if (right == 10)
        {
           
        }
    }
}